﻿using DevExpress.Utils.Win;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Demos.util;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DXApplicationTangche.UC.面料审核.Service;
using DXApplicationTangche.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.面料库存.入库
{
    public partial class Form保存面料成份 : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        private String composition;
        private String desc;
        private List<MaterialCompositionCountDTO> materialCompositionCountDTOs = new List<MaterialCompositionCountDTO>();
        public String fabcicComposition;
        private int contentCount;
        private String contentPercent;
        private FabricCompositionDTO fabricCompositionDTO = new FabricCompositionDTO();
        

        public Form保存面料成份(FabricCompositionDTO fabricCompositionDTO)
        {
            InitializeComponent();
            this.fabricCompositionDTO = fabricCompositionDTO;
            if (this.fabricCompositionDTO.materialCompositionCountDTOs.Count > 0)
            {
                this.gridControl面料成份一览.DataSource = this.fabricCompositionDTO.materialCompositionCountDTOs;
                this.materialCompositionCountDTOs = this.fabricCompositionDTO.materialCompositionCountDTOs;
            }
            this.searchLookUpEdit成份.Properties.DataSource = 面料审核Service.getMaterialCompositionAll();
            List<FabricContentDTO> fabricContentDTOs = new List<FabricContentDTO>();
            #region
            fabricContentDTOs.Add(new FabricContentDTO("100%"));
            fabricContentDTOs.Add(new FabricContentDTO("95%"));
            fabricContentDTOs.Add(new FabricContentDTO("90%"));
            fabricContentDTOs.Add(new FabricContentDTO("85%"));
            fabricContentDTOs.Add(new FabricContentDTO("80%"));
            fabricContentDTOs.Add(new FabricContentDTO("75%"));
            fabricContentDTOs.Add(new FabricContentDTO("70%"));
            fabricContentDTOs.Add(new FabricContentDTO("65%"));
            fabricContentDTOs.Add(new FabricContentDTO("60%"));
            fabricContentDTOs.Add(new FabricContentDTO("55%"));
            fabricContentDTOs.Add(new FabricContentDTO("50%"));
            fabricContentDTOs.Add(new FabricContentDTO("45%"));
            fabricContentDTOs.Add(new FabricContentDTO("40%"));
            fabricContentDTOs.Add(new FabricContentDTO("35%"));
            fabricContentDTOs.Add(new FabricContentDTO("30%"));
            fabricContentDTOs.Add(new FabricContentDTO("25%"));
            fabricContentDTOs.Add(new FabricContentDTO("20%"));
            fabricContentDTOs.Add(new FabricContentDTO("15%"));
            fabricContentDTOs.Add(new FabricContentDTO("10%"));
            fabricContentDTOs.Add(new FabricContentDTO("5%"));
            #endregion
            this.searchLookUpEdit含量.Properties.DataSource = fabricContentDTOs;
        }

        private void simpleButton添加_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.textBox修改含量.Text.ToString()))
            {
                MessageBox.Show("请选择含量");
                return;
            }
            if (String.IsNullOrEmpty(this.composition))
            {
                MessageBox.Show("成份不能为空");
                return;
            }
            //MaterialCompositionCountDTO materialCompositionCountDTO成份 = new MaterialCompositionCountDTO(this.composition, this.desc, this.contentPercent.Replace("%", "").ToString());
            MaterialCompositionCountDTO materialCompositionCountDTO成份 = new MaterialCompositionCountDTO(this.composition, this.desc, this.textBox修改含量.Text.ToString().Replace("%", "").ToString());

            if (this.materialCompositionCountDTOs.Count >= 1)
            {
                if (!this.materialCompositionCountDTOs.Exists(dto => dto.composition.Equals(materialCompositionCountDTO成份.composition)))
                {
                    this.materialCompositionCountDTOs.Add(materialCompositionCountDTO成份);
                    //this.contentCount += ValueUtil.StringToInt(this.contentPercent.Replace("%", "").ToString());
                    materialCompositionCountDTO成份 = null;
                    this.刷新();
                    return;
                }
                else
                {
                    MessageBox.Show("成份已存在，请勿重复添加");
                    return;
                }
            }
            this.materialCompositionCountDTOs.Add(materialCompositionCountDTO成份);
            //this.contentCount += ValueUtil.StringToInt(this.contentPercent.Replace("%", "").ToString());
            materialCompositionCountDTO成份 = null;
            this.刷新();
        }
        public void 刷新()
        {
            this.gridControl面料成份一览.DataSource = this.materialCompositionCountDTOs;
            this.gridView面料成份一览.RefreshData();
            this.fabcicComposition = this.textBox修改含量.Text.ToString().Replace("%", "").ToString()+"%" + this.composition+" ";
            this.fabricCompositionDTO.fabricComposition += this.fabcicComposition;
            this.fabricCompositionDTO.materialCompositionCountDTOs = this.materialCompositionCountDTOs;
            this.textBox修改含量.Clear();
        }
        #region
        private void searchLookUpEdit成份_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clear成份Btn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit成份View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit成份.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.composition = this.searchLookUpEdit成份.Properties.View.GetRowCellValue(rowHandle, "composition").ToString();
                    this.desc = this.searchLookUpEdit成份.Properties.View.GetRowCellValue(rowHandle, "desc").ToString();
                }
                //this.textEdit供货商.Text = this.fshortname;
                //this.textBox供应商名称.Text = this.fname;
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear成份Btn_Click(object sender, EventArgs e)
        {
            //this.searchLookUpEdit供应商.ToolTip = null;
            //searchLookUpEdit供应商.EditValue = null;
        }
        private void searchLookUpEdit成份_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
            {
                e.DisplayText = this.desc;
            }
        }
        #endregion
        #region
        private void searchLookUpEdit含量_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clear成份Btn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit含量View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit含量.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.contentPercent = this.searchLookUpEdit含量.Properties.View.GetRowCellValue(rowHandle, "content").ToString();
                }
                this.textBox修改含量.Text = this.contentPercent;
                //this.textBox供应商名称.Text = this.fname;
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear含量Btn_Click(object sender, EventArgs e)
        {
            //this.searchLookUpEdit供应商.ToolTip = null;
            //searchLookUpEdit供应商.EditValue = null;
        }
        private void searchLookUpEdit含量_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
            {
                e.DisplayText = this.contentPercent;
            }
        }
        #endregion


        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            new Form新增面料成份().ShowDialog();
        }

        private void Form保存面料成份_FormClosing(object sender, FormClosingEventArgs e)
        {
            foreach(MaterialCompositionCountDTO materialCompositionCountDTO in this.materialCompositionCountDTOs)
            {
                this.contentCount += ValueUtil.StringToInt(materialCompositionCountDTO.content);
            }
            if (!(100 == this.contentCount))
            {
                MessageBox.Show("面料成份含量不为100%");
                return;
            }
        }
    }
    class FabricContentDTO
    {
        public String content { get; set; }
        public FabricContentDTO()
        {

        }

        public FabricContentDTO(string content)
        {
            this.content = content;
        }
    }
}
